﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB ;
using YT.Controls;
using YT.Commons ;
using YT.Modules;

namespace YT.XH.SuddenDeafness
{
    public partial class frmSDExamBeforeTreatTitle : Form
    {
        private string mstrSickRecID = string.Empty;
        private FormStatus mfrmStatus;
        private string mstrTableName = "ExamBeforTreatResult";
        private string mstrSeqNo = string.Empty;
        ArrayList marrlist = new ArrayList();
        ArrayList marrlistCon = new ArrayList();
        private DBAccess mdbcon = new DBAccess(false, false);
       
        public frmSDExamBeforeTreatTitle()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
            mstrSeqNo ="1";
        }

        public frmSDExamBeforeTreatTitle(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mfrmStatus = FormStatus.AddNew;
        }

        public frmSDExamBeforeTreatTitle(string strSickRecID, FormStatus frmStauts, string strExamSeqNo)
        {
            mfrmStatus = frmStauts;
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mstrSeqNo = strExamSeqNo;
        }

        private void frmSDExamBeforeTreatTitle_Load(object sender, EventArgs e)
        {
            this.Text = "治疗前检查";
            FillData();
            timer1.Start();
        }

        private void btnExamInLab_Click(object sender, EventArgs e)
        {
            frmSDExamInLabTitle frmLab = new frmSDExamInLabTitle(mstrSickRecID,mstrSeqNo);
            frmLab.StartPosition = FormStartPosition.CenterScreen;
            frmLab.ShowDialog(); 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string strSeqNo = string.Empty;
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = string.Empty;           
            UpdateData(mstrSeqNo);
            mfrmStatus = FormStatus.Update;
            this.Close();
        }

        private void FindPanel(Control cons)
        {
            if (cons.Name == this.panel2.Name )
            {
                if (cons.GetType().Name == "Panel") marrlistCon.Add(cons.Name);
            }
            foreach (Control con in cons.Controls)
            {
                if (con.GetType().Name == "Panel" && con.Tag !="" && con.Tag != null)
                {
                    marrlist.Add(con);                   
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
                if (con.GetType().Name == "TextBox") con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }            
        }
        private void SetControl()
        {
            string strSQL = "select top 1 * from " + mstrTableName + " where  Type='" + TitleType.SuddenDeafness + "' and SickRecID='" + mstrSickRecID + "' and ExamSeqNo='" + mstrSeqNo + "'";
            DBAccess dbcon = new DBAccess(false, false);
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                panel2.Enabled = false;
            }
            dbcon.CloseCon();
        }
        private void FillData()
        {
            string strTableName = "ExamBeforTreatResult";
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
            }
            SetControl(); 
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    string strTitleType = TitleType.SuddenDeafness.ToString();
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            string strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  ExamSeqNo='"+ mstrSeqNo +"' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and Type='" + TitleType.SuddenDeafness  + "' and SickRecID='"+ mstrSickRecID  +"'";
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null) continue;
                            strResult = CvtFunc.ToString(drCheck["Result"]);
                            if (conText.GetType().Name == "TextBox" || conText.GetType().Name == "ComboBox"  )
                            {
                                conText.Text = strResult;
                                if (conText.Text != "") helpProd.SetHelpString(conText, conText.Text);                                  
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                if (strResult != "") ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult); 
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                continue;
                            }
                        }
                    }

                }
            }
            dbcon.CloseCon();
        }

        private void UpdateData(string strExamSeqNo)
        {
            string strTableName = "ExamBeforTreatResult";
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    DataRow drRow = null;
                    string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type,ExamSeqNo";
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            if (conText.GetType().Name == "TextBox" || conText.GetType().Name == "ComboBox")
                            {
                                strResult = conText.Text;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                strResult = BaseFunctions.FormatDate ( ((DateTimePicker)conText).Value.ToString (), DateFormatOption.DateFmtYMD );
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                strResult = (((RadioButton)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                strResult = (((CheckBox)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                strResult = "";
                                continue;
                            }
                            strColTitle = conText.Tag.ToString();

                            string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and Type='" + TitleType.SuddenDeafness  + "' and SickRecID='"+ mstrSickRecID  +"' and ExamSeqNo='"+ mstrSeqNo  +"'";
                            bool blnIsNew = false;
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null)
                            {
                                blnIsNew = true;
                                drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                            }
                            else
                            {
                                blnIsNew = false;
                                drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                            }

                            drRow["SickRecID"] = strSickRecID;
                            drRow["HdrSeqno"] = strHdrSeqNo;
                            drRow["DtlSeqNo"] = strDtlSeqNo;
                            drRow["RowNo"] = strColTitle;
                            drRow["Result"] = strResult;
                            drRow["Type"] = TitleType.SuddenDeafness  ;
                            drRow["ExamSeqNo"] = strExamSeqNo;
                            if (blnIsNew)
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                            else
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                        }
                    }
                }
            }
            dbcon.CloseCon();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmSDExamBeforeTreatTitle_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
            frmGifImage frmGIF = new frmGifImage();
            frmGIF.TopMost = true;
            frmGIF.Show();
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox133_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox128_TextChanged(object sender, EventArgs e)
        {

        }

        private void panel282_Paint(object sender, PaintEventArgs e)
        {

        }
        private void SelectNextText(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                SelectNextControl((Control)sender, true, true, true, true);
        }

        private void panel272_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox144_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox143_TextChanged(object sender, EventArgs e)
        {

        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void panel54_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
